/***************************************************************************
 *  Copyright (C) 2009, 2010 by Axel Jaeger <axeljaeger@googlemail.com>    *
 *                                                                         *
 *  This file is part of Glowworm.                                         *
 *                                                                         *
 *  Glowworm is free software: you can redistribute it and/or modify       *
 *  it under the terms of the GNU General Public License as published by   *
 *  the Free Software Foundation, version 3 of the License.                *
 *                                                                         *
 *  Glowworm is distributed in the hope that it will be useful,            *
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of         *
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          *
 *  GNU General Public License for more details.                           *
 *                                                                         *
 *  You should have received a copy of the GNU General Public License      *
 *  along with Glowworm.  If not, see <http://www.gnu.org/licenses/>.      *
 ***************************************************************************/

#include "HttpResponse.h"

#include <QIODevice>

HttpResponse::HttpResponse()
    : m_outputDevice(NULL)
    , m_headerSent(false)
{

}

HttpResponse::~HttpResponse()
{

}

QIODevice* HttpResponse::outputDevice() const
{
    return m_outputDevice;
}

void HttpResponse::setOutputDevice(QIODevice* device)
{
    m_outputDevice = device;
}

void HttpResponse::setStatusCode(HttpStatusCode sc)
{
    m_statusCode = sc;
}

HttpResponse::HttpStatusCode HttpResponse::statusCode() const
{
    return m_statusCode;
}

void HttpResponse::setMimeType(const QString & mimeType)
{
    m_mimeType = mimeType;
}

QString HttpResponse::mimeType() const
{
    return m_mimeType;
}

int HttpResponse::contentLength() const
{
    return m_contentLength;
}

void HttpResponse::setContentLength(int nsize)
{
    m_contentLength = nsize;
}

void HttpResponse::sendHeader()
{
    Q_ASSERT (headerSent() == false);
    Q_ASSERT (m_outputDevice != NULL);
    QString header(QString("HTTP/1.0 %1 %2\r\nContent-Type: %3\r\n\r\n")
                   .arg(statusCode())
                   .arg(statusVerbose(statusCode()))
                   .arg(mimeType()));


    if (statusCode() != HttpOK) {
        header += QString("<html><body><h1>%1 %2</h1></body></html>\r\n").arg(statusCode()).arg(statusVerbose(statusCode()));
    }

    outputDevice()->write(qPrintable(header));
    m_headerSent = true;
}

bool HttpResponse::headerSent() const
{
    return m_headerSent;
}

QString HttpResponse::statusVerbose(HttpStatusCode sc)
{
    switch (sc) {
        case HttpOK:
            return "OK";
        case HttpForbidden:
            return "Forbidden";
        case HttpFileNotFound:
            return "File not found";
        case HttpInternalServerError:
            return "Internal Server Error";
    }
    return QString::null;
}
